package com.zzx.bms.service.impl;

import com.zzx.bms.domain.Nursestation;
import com.zzx.bms.domain.NursestationExample;
import com.zzx.bms.persistence.NursestationMapper;
import com.zzx.bms.service.INursestationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by SamZhao on 2016/12/27.
 */
@Service(value="nursestationService")
public class NursestationServiceImpl implements INursestationService {

    @Autowired
    private NursestationMapper nursestationMapper;

    @Override
    public List<Nursestation> findAllNursestationList() {
        NursestationExample exp = new NursestationExample();
        NursestationExample.Criteria crt = exp.createCriteria();
        crt.andIsDeleteEqualTo("0");
        List<Nursestation> list = nursestationMapper.selectByExample(exp);
        return list;
    }

    @Override
    public int saveNursestation(Nursestation nursestation) {
        int flag = 0;
        if(nursestation.getId()!=null){
            flag = nursestationMapper.updateByPrimaryKeySelective(nursestation);
        }
        else{
            flag = nursestationMapper.insertSelective(nursestation);
        }
        return flag;
    }

    @Override
    public int deleteNursestationById(String id) {
        int flag = 0;
        NursestationExample exp = new NursestationExample();
        NursestationExample.Criteria crt = exp.createCriteria();
        Nursestation nursestation = nursestationMapper.selectByPrimaryKey(Integer.parseInt(id));
        if(nursestation!=null){
            nursestation.setIsDelete("1");
            flag = nursestationMapper.updateByPrimaryKeySelective(nursestation);
        }
        return flag;
    }

    @Override
    public int checkNursestation(Nursestation nursestation) {
        NursestationExample exp = new NursestationExample();
        NursestationExample.Criteria crt = exp.createCriteria();
        if(StringUtils.isNotEmpty(nursestation.getName()))
            crt.andNameEqualTo(nursestation.getName().trim());
        if(nursestation.getId()!=null)
            crt.andIdNotEqualTo(nursestation.getId());
        crt.andIsDeleteEqualTo("0");
        int flag = nursestationMapper.countByExample(exp);
        return flag;
    }

    @Override
    public Nursestation findNursestationById(String id) {
        Nursestation nursestation = null;
        if(StringUtils.isNotEmpty(id))
            nursestation = nursestationMapper.selectByPrimaryKey(Integer.parseInt(id));
        return nursestation;
    }
}
